/**
 * 6076. 表示一个折线图的最少线段数
 * https://leetcode.cn/contest/weekly-contest-294/problems/minimum-lines-to-represent-a-line-chart/
 */

/**
 * @param {number[][]} stockPrices
 * @return {number}
 */
function minimumLines(stockPrices) {
  if (stockPrices.length <= 2) {
    return stockPrices.length - 1;
  }
  stockPrices.sort((a, b) => a[0] - b[0]);
  let count = 1;
  for (let i = 2; i < stockPrices.length; i += 1) {
    const p1 = stockPrices[i - 2];
    const p2 = stockPrices[i - 1];
    const p3 = stockPrices[i];
    const x1 = BigInt(p1[0]);
    const y1 = BigInt(p1[1]);
    const x2 = BigInt(p2[0]);
    const y2 = BigInt(p2[1]);
    const x3 = BigInt(p3[0]);
    const y3 = BigInt(p3[1]);
    const area = x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2;
    if (area !== BigInt(0)) {
      count += 1;
    }
  }
  return count;
}

console.log(minimumLines([[1, 7], [2, 6], [3, 5], [4, 4], [5, 4], [6, 3], [7, 2], [8, 1]]) === 3);
console.log(minimumLines([[3, 4], [1, 2], [7, 8], [2, 3]]) === 1);
console.log(minimumLines([[1, 1000000000], [1000000000, 1000000000], [999999999, 1], [2, 999999999]]) === 3);
console.log(minimumLines([[1, 1], [499999999, 2], [999999998, 3], [1000000000, 5]]) === 3);
console.log(minimumLines([[1, 1], [500000000, 499999999], [1000000000, 999999998]]) === 2);
